/**
 * 
 */
$(function() {
	load();
});

function cols() {
	return [{
        field: 'state',
        checkbox: true,
        align: 'center',
        width : '20',
        valign: 'middle'
    },{
        field: 'name',
        title: '账单名称',
        align : 'center',
        width: '100'
    }, {
        field: 'balance',
        title: '收支方向',
        align : 'center',
        width: '75'
    }, {
        field: 'type',
        title: '账单类型',
        align : 'center',
        width: '100'
    }, {
        field: 'forwho',
        title: '账单归属',
        align : 'center',
        width: '100'
    }, {
        field: 'money',
        title: '账单金额',
        align : 'center',
        width: '100'
    }, {
        field: 'createtime',
        title: '创建日期',
        align : 'center',
        width: '75'
    }, {
    	formatter: operateFormatter,
    	events : operateEvents,
        title: '操作',
        align : 'center',
        width: '150'
    }];
}

function load() {
	$('#acount_table').bootstrapTable({
	    url: '/HomeAcount/acount/loadTable',
	    pagination:true,
	    pageNumber : 1,
	    pageSize : 5,
	    pageList : [5, 10, 20],
	    sidePagination:'server',//设置为服务器端分页
	    queryParams: queryParams,//参数
	    height : 400,
	    idField : "id",
	    columns: cols()
	});
}
/**
 * 查询参数
 * @param params
 * @returns
 */
function queryParams(params) {
	var data = $('#searchForm').serializeJson();
	params = $.mergeJsonObject(data,params);
	return params;
}

function operateFormatter(value, row, index) {
    return [
        '<a class="edit" href="javascript:void(0);" title="修改">',
        '<i class="glyphicon glyphicon-pencil"></i>',
        '</a>&nbsp;&nbsp;&nbsp;&nbsp;',
        '<a class="remove" href="javascript:void(0);" title="删除">',
        '<i class="glyphicon glyphicon-remove"></i>',
        '</a>&nbsp;&nbsp;&nbsp;&nbsp;',
        '<a class="view" href="javascript:void(0);" title="查看">',
        '<i class="glyphicon glyphicon-list-alt"></i>',
        '</a>'
    ].join('');
}

window.operateEvents = {
    'click .edit': function (e, value, row, index) {
    	$('#opFlag').val('2');//设置操作标志，2表示更新
    	$.ajax({
			url : "/HomeAcount/acount/loadAcountById",
			dataType : "json",
			type : "post",
			data : {
				id : row.id
			},
			success : function(data) {
				if(data) {
					var typeList = data.typeList;
					if(typeList) {
						var option = "<option value=''>---请选择---</option>";
						for(var i = 0; i < typeList.length; i++) {
							option += "<option value='"+typeList[i].id+"'>"+typeList[i].name+"</option>"
						}
						$('#acount_type').empty();
						$('#acount_type').append(option);
					}
					var formData = data.acount;
					$('#acountForm').form('load', formData);
					$('#acountModal').modal({
			    		keyboard: false,
			    		show : true
					});
				}else {
					$(window.parent.alert("操作出错，请联系管理员！"));  
				}
			},
			error : function() {
				$(window.parent.alert("操作出错，请联系管理员！"));  
			}
		});
    },
    'click .remove': function (e, value, row, index) {
    	$.ajax({
			url : "/HomeAcount/acount/deleteAcount",
			dataType : "json",
			type : "post",
			data : {
				id : row.id
			},
			success : function(data) {
				if(data.flag) {
					$(window.parent.alert("删除成功！"));
					$('#acount_table').bootstrapTable('refresh', '');
					$('#acount_table').bootstrapTable('remove', {
			            field: 'id',
			            values: row.id
			        });
				}else {
					$(window.parent.alert("删除失败！"));
				}
			},
			error : function() {
				$(window.parent.alert("操作出错，请联系管理员！"));
			}
		});
    },
    'click .view' : function(e, value, row, index) {
    	$('#opFlag').val('0');//设置操作标志，0表示查看
    	$.ajax({
			url : "/HomeAcount/acount/loadAcountById",
			dataType : "json",
			type : "post",
			data : {
				id : row.id
			},
			success : function(data) {
				if(data) {
					var typeList = data.typeList;
					if(typeList) {
						var option = "<option value=''>---请选择---</option>";
						for(var i = 0; i < typeList.length; i++) {
							option += "<option value='"+typeList[i].id+"'>"+typeList[i].name+"</option>"
						}
						$('#acount_type').empty();
						$('#acount_type').append(option);
					}
					var formData = data.acount;
					$('#acountForm').form('load', formData);
					$('#acountForm').find('input[type=text]').each(function(){
						$(this).attr('readonly','readonly');
					});
					$('#acountForm').find('select').each(function(){
						$(this).attr('disabled','disabled');
					});
					$('#acountForm').find('textarea').each(function(){
						$(this).attr('readonly','readonly');
					});
					$('#formSave').hide();
					$('#acountModal').modal({
			    		keyboard: false,
			    		show : true
					});
				}else {
					$(window.parent.alert("操作出错，请联系管理员！"));  
				}
			},
			error : function() {
				$(window.parent.alert("操作出错，请联系管理员！"));  
			}
		});
    }
};


function setTypeValue(obj,t) {
	var value = $(obj).val();
	if(value) {
		$.ajax({
			url : "/HomeAcount/acount/loadAcountType",
			dataType : "json",
			type : "post",
			data : {
				balance : value
			},
			success : function(data) {
				if(data.length > 0) {
					var option = "<option value=''>---请选择---</option>";
					for(var i = 0; i < data.length; i++) {
						option += "<option value='"+data[i].id+"'>"+data[i].name+"</option>";
					}
					$('#'+t).empty();
					$('#'+t).append(option);
				}
			},
			error : function() {
				$(window.parent.alert("操作出错，请联系管理员！"));  
			}
		});
	}else {
		var option = "<option value=''>---请选择---</option>";
		$('#'+t).empty();
		$('#'+t).append(option);
	}
}

function checkBalance(type) {
	if(type == '1') {
		var value = $("#balance").val();
		if(!value) {
			$(window.parent.alert("请您先选择收支方向！"));  
		}
	}else {
		var value = $("#acount_balance").val();
		if(!value) {
			$(window.parent.alert("请您先选择收支方向！"));
		}
	}
	
}

function formReset(formId) {
	$('#'+formId).form('reset');
}

function modelColse() {
	$('#acountForm').form('reset');
	var flag = $('#opFlag').val();
	if(flag == '0') {
		$('#acountForm').find('input[type=text]').each(function(){
			$(this).removeAttr('readonly');
		});
		$('#acountForm').find('select').each(function(){
			$(this).removeAttr('disabled');
		});
		$('#acountForm').find('textarea').each(function(){
			$(this).removeAttr('readonly');
		});
	}
	$('#formSave').show();
}

function doSearch() {
	var data = $('#searchForm').serializeJson();
	$('#acount_table').bootstrapTable('refresh', {query: data});
}

function addAcount() {
	$('#opFlag').val('1');//设置操作标志，1表示新增
	$('#acount_type').empty().append("<option value=''>---请选择---</option>");
	$('#acountModal').modal('show');
}

function acountSave() {
	var opFlag = $('#opFlag').val();
	var data = $('#acountForm').serializeJson();
	if(opFlag == "2") {
		$.ajax({
			url : "/HomeAcount/acount/updateAcount",
			dataType : "json",
			type : "post",
			data : data,
			success : function(data) {
				if(data.flag) {
					$('#acountModal').modal('hide');
					formReset('acountForm');
					$(window.parent.alert("更新成功！"));
					$('#acount_table').bootstrapTable('refresh', '');
				}else {
					$(window.parent.alert("更新失败！"));
				}
			},
			error : function() {
				$(window.parent.alert("操作出错，请联系管理员！"));
			}
		});
	}
	if(opFlag == "1") {
		$.ajax({
			url : "/HomeAcount/acount/addAcount",
			dataType : "json",
			type : "post",
			data : data,
			success : function(data) {
				if(data.flag) {
					$('#acountModal').modal('hide');
					formReset('acountForm');
					$(window.parent.alert("新增成功！"));
					$('#acount_table').bootstrapTable('refresh', '');
				}else {
					$(window.parent.alert("新增失败！"));
				}
			},
			error : function() {
				$(window.parent.alert("操作出错，请联系管理员！"));
			}
		});
	}
	
}